package com.student.xujiang.mysmalluse.companylogin.retrofit2protogenesis;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.student.xujiang.R;
import com.student.xujiang.mysmalluse.companylogin.model.AccessToken;
import com.student.xujiang.mysmalluse.companylogin.model.LoginRequest;
import com.student.xujiang.mysmalluse.companylogin.model.LoginResponse;
import com.student.xujiang.mysmalluse.companylogin.model.RegisterRequest;
import com.student.xujiang.mysmalluse.companylogin.model.RegisterResponse;
import com.student.xujiang.mysmalluse.companylogin.model.Urls;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import retrofit2.converter.jackson.JacksonConverterFactory;

public class CLoginMainActivity extends AppCompatActivity {


    @BindView(R.id.editText)
    EditText editText;   //name
    @BindView(R.id.editText2)
    EditText editText2;  //password
    @BindView(R.id.bt_clm_login)
    Button btClmLogin;
    @BindView(R.id.bt_clm_register)
    Button btClmRegister;
    @BindView(R.id.tv_logiorregister_result)
    TextView tvLogiorregisterResult;
    @BindView(R.id.activity_clogin_main)
    LinearLayout activityCloginMain;

    private Retrofit retrofit;


    private Observable<AccessToken> observable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clogin_main);
        ButterKnife.bind(this);

    retrofit = new Retrofit
            .Builder()
            .baseUrl(Urls.BaseUrl)
            .addConverterFactory(JacksonConverterFactory.create())
            .build();
//    Service2 service2 = retrofit.create(Service2.class);
//    Call<AccessToken> list = service2.getList(, 0, 1, 20);
//    list.enqueue(this);


    }

    @OnClick({R.id.bt_clm_login, R.id.bt_clm_register})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_clm_login:
                String name1 = editText.getText().toString().trim();
                String password1 = editText2.getText().toString().trim();
                //password1 = MD5Utils.md5ToString(password1);
                LoginRequest loginRequest =new LoginRequest(CLoginMainActivity.this,name1,password1,"youmengdedisnafangtuisong");
                UserService userService1 = retrofit.create(UserService.class);
                Call<LoginResponse> call1 = userService1.login(loginRequest);
                call1.enqueue(

                        new Callback<LoginResponse>() {
                            @Override
                            public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
                                if(!response.isSuccessful()){
                                    ResponseBody responseBody = response.errorBody();
                                    try {
                                        Log.i("TAG",responseBody.string());
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                    return ;
                                }else{
                                    LoginResponse login = response.body();
                                    tvLogiorregisterResult.setText(login.toString());
                                    Log.i("TAG",response.body().toString());
                                    //12、发送粘性事件

                                    EventBus.getDefault().postSticky(new MessageEvent(login));

                                    Intent intent = new Intent(CLoginMainActivity.this,UserRecyclerViewActivity.class);
                                    startActivity(intent);

                                }
                            }

                            @Override
                            public void onFailure(Call<LoginResponse> call, Throwable t) {
                                Log.i("tag",call.toString());
                                Log.i("tag",t.getLocalizedMessage());
                            }
                        });


                break;
            case R.id.bt_clm_register:
                String name = editText.getText().toString().trim();
                String password = editText2.getText().toString().trim();
                RegisterRequest registerRequest =new RegisterRequest(CLoginMainActivity.this,name,password,"youmengdedisnafangtuisong");
                UserService userService = retrofit.create(UserService.class);
                Call<RegisterResponse> call = userService.register(registerRequest);
                call.enqueue(new Callback<RegisterResponse>() {
                    @Override
                    public void onResponse(Call<RegisterResponse> call, Response<RegisterResponse> response) {
                        if(!response.isSuccessful()){
                            ResponseBody responseBody = response.errorBody();
                            try {
                                Log.i("TAG",responseBody.string());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            return ;
                        }else{
                            RegisterResponse token = response.body();
                            tvLogiorregisterResult.setText(token.toString());
                            Log.i("TAG",response.body().toString());
                        }
                    }

                    @Override
                    public void onFailure(Call<RegisterResponse> call, Throwable t) {
                        Log.i("tag",call.toString());
                        Log.i("tag",t.getLocalizedMessage());
                    }
                });
                break;
        }
    }


}
